package com.wink_172.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.wink_172.library.R;
import com.wink_172.library.model.Constants;
import com.wink_172.library.utils.DisplayUtil;

import java.util.HashSet;

public
/**
 倒计时控件
 可以自己定义样式   圆形或者矩形 啥都行
请不要清空中间文字  可以拿view遮挡住即可

 使用方法
 android:id="@+id/circle_time_view"

 binding!!.circleTimeView.setCallback(object : CircleTimeView.ICallback {
 override fun onSendEvent(event: Int, vararg args: Any?) {
 if (event == Constants.EVENT_01) {
 }
 }
 })

 binding!!.circleTimeView.startCountDown(15)


 待删除
 */
 class CircleTimeView extends View {
    //圆轮颜色
    public int mRingColor;
    //圆轮宽度
    public  float mRingWidth;
    //圆轮进度值文本大小
    public   int mRingProgessTextSize;
    //宽度
    public  int mWidth;
    //高度
    public  int mHeight;
    public  Paint mPaint;
    //圆环的矩形区域
    public RectF mRectF;
    //
    public  int mProgessTextColor;
    public  int mAllCountdownTime = 0;//总时间
    public  int mMillisUntilFinished = 0;//剩余时间
    public  float mCurrentProgress;

    public MyHandler mHandler;

    public MyHandler getHandler() {
        return mHandler;
    }


    public void setCallback(ICallback callback) {
        this.mCallback = callback;
    }

    public ICallback mCallback;

    public interface ICallback {
        void onSendEvent(int event, Object... args);
    }


//使用方法
//          (binding!!.circleTimeView as CircleTimeView).setCallback(object : CircleTimeView.ICallback {
//        override fun onSendEvent(event: Int, vararg args: Any) {
//            if (event == Constants.LEFT_BTN) {
//                finish()
//            }
//        }
//    })


    public CircleTimeView(Context context) {
        this(context, null);
    }

    public CircleTimeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRingColor = context.getResources().getColor( R.color.logcat_level_info_color);
        mRingWidth = 3;
        mRingProgessTextSize = DisplayUtil.sp2px(context, 20);
        mProgessTextColor = context.getResources().getColor(R.color.logcat_level_error_color);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);

        mHandler = new MyHandler();
        this.setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        mRectF = new RectF(0 + mRingWidth / 2, 0 + mRingWidth / 2,
                mWidth - mRingWidth / 2, mHeight - mRingWidth / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //颜色
        mPaint.setColor(mRingColor);
        //空心
        mPaint.setStyle(Paint.Style.STROKE);
        //宽度
        mPaint.setStrokeWidth(mRingWidth);
        canvas.drawArc(mRectF, -90, mCurrentProgress - 360, false, mPaint);//圆形

//        canvas.drawRoundRect(mRectF, 20, 60, mPaint);//矩形

        //绘制文本
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        String text = mAllCountdownTime - (int) (mCurrentProgress / 360f * mAllCountdownTime) + "";
        textPaint.setTextSize(mRingProgessTextSize);
        textPaint.setColor(mProgessTextColor);

        //文字居中显示
        Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
        int baseline = (int) ((mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2);

        canvas.drawText(text, mRectF.centerX(), baseline, textPaint);
    }


    /**
     * 暂停倒计时
     */
    public void onPause() {
        if (mHandler != null) {
            mHandler.removeMessages(Constants.EVENT_01);
        }
    }

    /**
     * 结束倒计时
     */
    public void onStop() {
        if (mHandler != null) {
            mHandler.removeMessages(Constants.EVENT_01);
            mAllCountdownTime = 0;
            mMillisUntilFinished = 0;
            setVisibility(GONE);

        }
    }

    private static final String TAG = "CircleTimeView";

    /**
     * 开始倒计时
     */
    public void startCountDown(final int cutDownTime) {
        mAllCountdownTime = cutDownTime;
        mMillisUntilFinished = cutDownTime;
        setVisibility(VISIBLE);

        Message message = new Message();
        message.what = Constants.EVENT_01;
        message.arg1 = mAllCountdownTime;

        mHandler.sendMessage(message);

    }


    public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.EVENT_01:
                    mMillisUntilFinished = msg.arg1 - 1;

                    if (mMillisUntilFinished == 0) {
                        mAllCountdownTime = 0;
                        mMillisUntilFinished = 0;
                        mCurrentProgress = 0;
                        invalidate();

                        if (mCallback != null) {
                            mCallback.onSendEvent(Constants.EVENT_02);
                        }

                    } else {
                        mCurrentProgress = 360 * ((float) (mAllCountdownTime - mMillisUntilFinished) / (float) mAllCountdownTime);
                        invalidate();

                        Message message = new Message();
                        message.what = Constants.EVENT_01;
                        message.arg1 = mMillisUntilFinished;
                        message.arg2 = msg.arg2;
                        mHandler.sendMessageDelayed(message, 20);
                    }
                    break;
            }
        }
    }


}